﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace SanctusSoftware.Music.ChordSheetEditor.Word
{
    public static class SoftSentenceHelper
    {

        #region Soft Sentences
		
        public static List<Range> GetSoftSentences(Sentences sentences)
        {
            List<Range> softSentences = new List<Range>();

            for (int i = 1; i <= sentences.Count; i++)
            {
                // Current Sentence
                Range sentence = sentences[i];
                softSentences.AddRange(GetSoftSentences(sentence));
            }

            return softSentences;

        }

        public static List<Range> GetSoftSentences(Range sentence)
        {
            List<Range> softSentences = new List<Range>();
            
            if (!sentence.Text.Contains('\v'))
            {
                // I don't like this work-around... pasting from a web page (which doens't have \v) 
                // can cause weird problems, like sentence.End - sentence.Start != sentence.Text.Length
                // If a web page did have \v in it, this code would break.
                object rangeStartTemp = sentence.Start;
                object rangeEndTemp = sentence.End;
                Range temp = sentence.Document.Range(ref rangeStartTemp, ref rangeEndTemp);
                softSentences.Add(temp);
            }
            else
            {
                string[] softSentencesText = sentence.Text.Split(new char[] { '\v' }, StringSplitOptions.None);

                // Initial Values for First Soft Sentence
                int rangeStart = sentence.Start;


                //foreach (string softSentenceText in softSentencesText)
                for (int i = 0; i < softSentencesText.Length; i++)
                {

                    string softSentenceText = softSentencesText[i];

                    int rangeEnd = rangeStart + softSentenceText.Length;
                    if (i < (softSentencesText.Length - 1))
                    {
                        // Keep the Vertical Tab at the End of the Line
                        rangeEnd += "\v".Length;
                    }

                    object rangeStartTemp = rangeStart;
                    object rangeEndTemp = rangeEnd;
                    Range temp = sentence.Document.Range(ref rangeStartTemp, ref rangeEndTemp);
                    softSentences.Add(temp);

                    // Values for Next Soft Sentence
                    rangeStart += softSentenceText.Length + "\v".Length;
                }
            }
            return softSentences;
        }

        #endregion

    }
}
